package com.gdiot.smart_scale;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public class GoodsRecycerAdapter extends RecyclerView.Adapter<GoodsRecycerAdapter.ViewHolder> {
    Context context;
    List<goods> list;
    private ItemClickListener mItemClickListener ;

    // 利用接口 -> 给RecyclerView设置点击事件
    public interface ItemClickListener{
        public void onItemClick(int position) ;
    }
    public void setOnItemClickListener(ItemClickListener itemClickListener){
        this.mItemClickListener = itemClickListener ;

    }

    public GoodsRecycerAdapter(SellActivity sellActivity, List<goods> goodsList) {
        this.context = sellActivity;
        this.list = goodsList;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView ITEM_PIC;
        TextView  ITEM_NAME,ITEM_PRICE;
        ConstraintLayout ITEM_LAYOUT;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ITEM_PIC = itemView.findViewById(R.id.item_goods_pic);
            ITEM_NAME = itemView.findViewById(R.id.item_goods_name);
            ITEM_PRICE = itemView.findViewById(R.id.item_goods_price);
            ITEM_LAYOUT = itemView.findViewById(R.id.itemlayout);
        }
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final  View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods,parent,false);
        final  ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        goods mygoods = list.get(position);
        holder.ITEM_NAME.setText( mygoods.getName());
        holder.ITEM_PRICE.setText( mygoods.getGoodsPrice());
        Glide.with(context).load(mygoods.getPicString()).into(holder.ITEM_PIC);

        // 点击事件一般都写在绑定数据这里，当然写到上边的创建布局时候也是可以的
        if (mItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 这里利用回调来给RecyclerView设置点击事件
                    mItemClickListener.onItemClick(position);
                }
            });
        }

/*
        // 给RecyclerView中item中的单独控件设置点击事件 可以直接在adapter中使用setOnClickListener即可
        holder.ITEM_LAYOUT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context , "position -> "+position , Toast.LENGTH_SHORT).show();
            }
        });
*/
    }

    @Override
    public int getItemCount() {

        return list.size();
    }


}
